package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    void insert(Orders orders);


    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    @Update("update orders set status=#{orderStatus},pay_status=#{orderPaidStatus},checkout_time=#{check_out_time} " +
            "where number=#{orderNumber}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, String orderNumber);


    Page<Orders> select(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     *查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id=#{id}")
    Orders selectByid(Long id);


    /**
     * 查询所有订单
     * @return
     */
    @Select("select * from orders")
    List<Orders> selectAll();

    @Select("select * from orders where status=#{pendingPayment} and order_time <= #{time}")
    List<Orders> selectTimeOutPayOrder(Integer pendingPayment, LocalDateTime time);

    @Select("SELECT  * from orders where number=#{orderNumber}")
    Long selectIdByNumber(String orderNumber);

    Double Turnover(Map map);



    /**
     * 订单报表
     * @param date
     * @param aNull
     * @return
     */
    Integer getOrderStatistics(LocalDate date, Integer completed);

    /**
     * 统计销量排名---通过时间段得到orderId
     * @param begin
     * @param end
     * @param completed
     * @return
     */

    @Select("SELECT id FROM orders WHERE DATE(order_time) >= #{begin} AND  DATE(order_time) <= #{end} AND status=#{completed};")
    List<Long> selectOrderIdBytime(LocalDate begin, LocalDate end, Integer completed);

    Integer countByMap(Map map);

    Double sumByMap(Map map);
}
